package org.kpli.jak.net.second;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

public class ClientThread extends Thread {

	private DataInputStream is = null;
	private PrintStream os = null;
	private Socket clientSocket = null;
	private ClientThread clienThread[];

	public ClientThread(Socket clientSocket, ClientThread[] thread) {
		this.clientSocket = clientSocket;
		this.clienThread = thread;
	}

	@SuppressWarnings("deprecation")
	public void run() {
		String line;
		String name;
		try {
			is = new DataInputStream(clientSocket.getInputStream());
			os = new PrintStream(clientSocket.getOutputStream());
			os.println("Masukkan Nama Anda : ");
			name = is.readLine();
			os.println("Hello "
					+ name
					+ " anda telah bergabung di chat room.\nuntuk keluar dari room masukkan /quit pada baris baru");
			for (int i = 0; i <= 9; i++)
				if (clienThread[i] != null && clienThread[i] != this)
					clienThread[i].os.println("*** User Baru " + name
							+ " masuk di chat room !!! ***");
			while (true) {
				line = is.readLine();
				if (line.startsWith("/quit"))
					break;
				for (int i = 0; i <= 9; i++)
					if (clienThread[i] != null)
						clienThread[i].os.println("<" + name + "> " + line);
			}
			for (int i = 0; i <= 9; i++)
				if (clienThread[i] != null && clienThread[i] != this)
					clienThread[i].os.println("*** User " + name
							+ " telah meninggalkan room !!! ***");

			os.println("*** Bye " + name + " ***");

			for (int i = 0; i <= 9; i++)
				if (clienThread[i] == this)
					clienThread[i] = null;
			is.close();
			os.close();
			clientSocket.close();
		} catch (IOException e) {
		}
		;
	}

}
